<template>
	<view class="detail-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="发起远程会议"></uni-navbar></view>

		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="form-item" required label="会议名称" prop="roomName">
					<u-input v-model="form.roomName" type="text" placeholder="请输入会议名称" />
				</u-form-item>
				<u-form-item class="form-item" required label="开始时间" prop="hopeStartTime">
					<date-time-picker placeholder="请选择开始时间" fields="minute" @change="timeChange($event, 'hopeStartTime')"></date-time-picker>
				</u-form-item>
				<u-form-item class="form-item" required label="开始时间" prop="hopeEndTime">
					<date-time-picker placeholder="请选择开始时间" fields="minute" @change="timeChange($event, 'hopeEndTime')"></date-time-picker>
				</u-form-item>

				<u-form-item class="form-item" required label="主持人">
					<u-input v-model="zxName" type="select" :select-open="zxShow" placeholder="请选择主持人" @click="zxShow = true" />
					<select-tree ref="zxTree" :show="zxShow" :range="zxList" @confirm="treeConfirm($event, 'zx')" @cancel="treeCancel($event, 'zx')"></select-tree>
				</u-form-item>

				<u-form-item class="form-item" required label="参会人员">
					<u-input v-model="chName" type="select" :select-open="chShow" placeholder="请选择参会人员" @click="chShow = true" />
					<select-tree ref="chTree" :show="chShow" :range="chList" @confirm="treeConfirm($event, 'ch')" @cancel="treeCancel($event, 'ch')"></select-tree>
				</u-form-item>
			</u-form>
		</view>

		<view class="fiexd-btm"><u-button class="form-btn" type="error" @click="confirm">提交</u-button></view>
	</view>
</template>

<script>
import dateTimePicker from '@/components/uni-datetime-picker';
import selectTree from '@/components/uni-select-tree.vue';
import { mapState } from 'vuex';
export default {
	components: {
		dateTimePicker,
		selectTree
	},
	data() {
		return {
			zxIds: '',
			zxName: '',
			zxShow: false,
			zxList: [],

			chIds: '',
			chName: '',
			chShow: false,
			chList: [],

			form: {
				roomName: '',
				hopeStartTime: '',
				hopeEndTime: '',
				clientCode: '',
				createBy: '',
				waihots: ''
			},
			rules: {
				roomName: [
					{
						required: true,
						message: '请输入会议名称',
						trigger: 'change'
					}
				],
				hopeStartTime: [
					{
						required: true,
						message: '请输入开始时间',
						trigger: 'change'
					}
				],
				hopeEndTime: [
					{
						required: true,
						message: '请输入结束时间',
						trigger: 'change'
					}
				]
			}
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadTeam('weiyuan, changwei');
	},
	onReady() {
		this.$refs.uForm.setRules(this.rules);
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		categoryGroupChange(e) {
			this.form.category = e;
		},
		typeGroupChange(e) {
			this.form.type = e;
		},
		treeConfirm(e, type) {
			if (!e || e.length == 0) {
				this[type + 'Show'] = false;
				this[type + 'Name'] = '';
				this[type + 'Ids'] = [];
				return false;
			}
			this[type + 'Ids'] = [];
			let names = [];
			let i = 0;
			e.forEach((item) => {
				this[type + 'Ids'].push(item.id);
				if (i < 3) {
					names.push(item.name);
					i++;
				}
			});
			this[type + 'Name'] = names.join(',') + '等' + e.length + '人';
			this[type + 'Show'] = false;
		},
		treeCancel(e, type) {
			this[type + 'Show'] = false;
		},
		timeChange({ date, dt }, idx) {
			this.form[idx] = date.year + '-' + date.month2 + '-' + date.day2 + ' ' + date.hour2 + ':' + date.minute2 + ':' + '00';
		},
		selectConfirm(e, idx) {
			this[idx] = e[0].label;
			this.form[idx] = e[0].value;
		},
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;
			} else {
				this.$logout();
			}
		},
		loadTeam(type) {
			this.$request('/system/sys-user/team_users', {
				clientCode: this.clientCode,
				currentUserId: this.userId,
				dictType: 'jiebie',
				roleCodes: type,
				teamType: '2',
				withCw: '1'
			}).then((res) => {
				this.zxList = res.data;
				this.chList = res.data;
			});
		},
		confirm() {
			this.$refs.uForm.validate((valid) => {
				if (!valid) {
					return false;
				}
				let data = {};
				this.form.waihots = this.userId;
				this.form.clientCode = this.clientCode;
				this.form.createBy = this.userId;
				if (this.zxIds.length == 0) {
					this.$msg('请选择主持人');
					return false;
				}
				if (this.chIds.length == 0) {
					this.$msg('请选择参会人员');
					return false;
				}

				let arr = this.chIds.filter((item) => {
					return this.zxIds.indexOf(item) > -1;
				});
				if (arr && arr.length > 0) {
					this.$msg('选择人员不能重复');
					return false;
				}

				data = {
					ycxsHstRoom: this.form,
					chUsers: this.chIds.join(','),
					zxUsers: this.zxIds.join(',')
				};
				this.$request('/haoshitong/add', data, {
					load: true
				}).then((res) => {
					this.$msg(res.msg);
					setTimeout(() => {
						let pre = this.$prePage();
						if (pre && pre.loadData) {
							pre.loadData('refresh');
						}
						uni.navigateBack();
					}, 500);
				});
			});
		}
	}
};
</script>

<style lang="scss">
.detail-view {
	background-color: #fff;
}

.form-box {
	.form-item {
		@include mp-flex;
		border-bottom: 20rpx solid $u-border-color;
	}
}

.fiexd-btm {
	position: static;
	padding: 30rpx;
}
</style>
